<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Database for Clinician
 * @author Michael Lee
 * @published March 19, 2014
 */

include_once('dbinfo.php');
include_once(dirname(__FILE__) . '/../domain/Clinician.php');

/*function create_dbClinician(){
    connect();
    mysql_query("DROP TABLE IF EXISTS dbClinician");
    $result = mysql_query("CREATE dbClinician (id TEXT NOT NULL, fname TEXT NOT NULL, lname TEXT NOT NULL, specialty TEXT NOT NULL, phoneNum VARCHAR(10), cellNum VARCHAR(10), SSN TEXT NOT NULL, DOB TEXT NOT NULL, age VARCHAR(3)");
    if (!result)
        echo mysql_error() . "Error creating dbClinician table<br>";
} */

function add_clinician($person) {
    error_log('in add_clinician');
    if (!$person instanceof Clinician)
        die("Error: add_clinician type mismatch");
    connect();
    $query = "SELECT * FROM dbClinician WHERE clinicianID = '" . $person->get_id() . "'";
    error_log('checking if record exists, query is'.$query);
    $result = mysql_query($query);
     if (!$result)
    {
        error_log('ERROR on select in add_clinician '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    if ($result == null || mysql_num_rows($result) == 0) {
        $insertQuery='INSERT INTO dbClinician (clinicianID,first_name,last_name,specialty,availability,phone1,phone2,ssn,npi) '
                . 'VALUES("' .
                $person->get_id() . '","' .
                $person->get_fname() . '","' .
                $person->get_lname() . '","' .
                $person->get_specialty() . '","' .
                $person->get_availability() . '","' .
                $person->get_phoneNum() . '","' .
                $person->get_cellNum() . '","' .
                $person->get_SSN() . '","' .
                $person->get_npi() . 
                '");';
        error_log('insert query is'.$insertQuery);
        $result = mysql_query($insertQuery);
         if (!$result) {
           error_log("insert failed in add_clinician " . mysql_error());
           die();
    }
        mysql_close();
        return true;
    }
    mysql_close();
    return false;
}

function remove_clinician($id) {
    connect();
    $query = 'SELECT * FROM dbClinician WHERE clinicianID = "' . $id . '"';
    $result = mysql_query($query);
    if ($result == null || mysql_num_rows($result) == 0) {
        mysql_close();
        return false;
    }
    $query = 'DELETE FROM dbClinician WHERE clinicianID = "' . $id . '"';
    $result = mysql_query($query);
    mysql_close();
    return true;
}

function retrieve_clinician($id) {
    connect();
    $query = "SELECT * FROM dbClinician WHERE clinicianID = '" . $id . "'";
    error_log('in retrieve_clinician, query is '. $query);
    $result = mysql_query($query);
     if (!$result)
    {
        error_log('ERROR on select in retrieve_clinician '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    if (mysql_num_rows($result) !== 1) {
        mysql_close();
        return false;
    }
    $result_row = mysql_fetch_assoc($result);
    $theClinician = make_a_clinician($result_row);
    return $theClinician;
}

function retrieve_clinician_by_name($name) {
    $clinicianList = array();
    if (!isset($name) || $name == "" || $name == null)
        return $clinicianList;
    connect();
    $name = explode(" ", $name);
    $fname = $name[0];
    $lname = $name[1];
    $query = "SELECT * FROM dbClinician WHERE first_name = '" . $fname . "' AND last_name = '". $lname ."'";
     error_log('in retrieve_clinician_by_name, query is '. $query);
    $result = mysql_query($query);
     if (!$result)
    {
        error_log('ERROR on select in retrieve_clinician_by_name '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    while ($result_row = mysql_fetch_assoc($result)) {
        $the_clinician = make_a_clinician($result_row);
        $clinicianList[] = $the_clinician;
    }
    return $clinicianList;
}

function retrieve_clinician_by_specialty($specialty) {
    error_log("in retrieve_clinician_by_specialty looking for ".$specialty);
        $clinicianList = array();
    if (!isset($specialty) || $specialty == "" || $specialty == null)
        return $clinicianList;
    connect();
    $query = "SELECT * FROM dbClinician WHERE specialty = '" . $specialty . "'";
    error_log('in retrieve_clinician_by_specialty, query is '. $query);
    $result = mysql_query($query);
     if (!$result)
    {
        error_log('ERROR on select in retrieve_clinician_by_specialty '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    if (mysql_num_rows($result) !== 1) {
        mysql_close();
        return false;
    }
    while ($result_row = mysql_fetch_assoc($result)) {
        $the_clinician = make_a_clinician($result_row);
        $clinicianList[] = $the_clinician;
    }
    return $clinicianList;
}

function retrieve_clinician_by_ssn($SSN) {
    connect();
    $query = "SELECT * FROM dbClinician WHERE SSN = '" . $SSN . "'";
    $result = mysql_query($query);
    if (mysql_num_rows($result) !== 1) {
        mysql_close();
        return false;
    }
    $result_row = mysql_fetch_assoc($result);
    $theClinician = make_a_clinician($result_row);
    return $theClinician;
}

function retrieve_clinician_by_npi($NPI) {
    connect();
    $query = "SELECT * FROM dbClinician WHERE NPI = '" . $NPI . "'";
    $result = mysql_query($query);
    if (mysql_num_rows($result) !== 1) {
        mysql_close();
        return false;
    }
    $result_row = mysql_fetch_assoc($result);
    $theClinician = make_a_clinician($result_row);
    return $theClinician;
}

function make_a_clinician ($result_row){
    $theClinician = new Clinician(
            $result_row['first_name'],
            $result_row['last_name'],
            $result_row['specialty'],
              $result_row['phone1'],
              $result_row['phone2'],
             $result_row['ssn'],
            $result_row['availability'],   
            $result_row['npi']);
    return $theClinician;
}

function getall_dbClinician() {
    connect();
    $query = "SELECT * FROM dbClient ORDER BY lname,fname";
    $result = mysql_query($query);
    if ($result == null || mysql_num_rows($result) == 0) {
        mysql_close();
        return false;
    }
    $result = mysql_query($query);
    $theCliniciant = array();
    while ($result_row = mysql_fetch_assoc($result)) {
        $clinician = make_a_clinician($result_row);
        $theCliniciant[] = $clinician;
    }

    return $theClinician;
}